package com.hosp.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hosp.bean.Doctor;
import com.hosp.bean.Hosregister;
import com.hosp.bean.HosregisterExample;
import com.hosp.controller.dto.RegistrationDTO;
import com.hosp.mapper.DoctorMapper;
import com.hosp.mapper.HosregisterMapper;
import com.hosp.vo.PageRequestVO;
import com.hosp.vo.RegistrationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ztranscript 2021/10/02 1:07
 **/
@Slf4j
@Service
public class RegistrationService {
    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private HosregisterMapper hosregisterMapper;

    /**
     * 查询列表
     *
     * @param registrationDTO 查询条件
     * @param pageRequest     分页条件
     * @return 响应
     */
    public PageInfo<RegistrationVO> list(RegistrationDTO registrationDTO, PageRequestVO pageRequest) {
        HosregisterExample example = new HosregisterExample();
        HosregisterExample.Criteria criteria = example.createCriteria();
        if (registrationDTO.getHosrId() != null) {
            criteria.andHosrIdEqualTo(registrationDTO.getHosrId());
        }

        // 分页查询
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<Hosregister> hosregisters = hosregisterMapper.selectByExample(null);
        PageInfo<Hosregister> hosregisterPageInfo = new PageInfo<>(hosregisters);

        // 查找对应的医生信息
        List<RegistrationVO> registrationVOList = new ArrayList<>();
        for (Hosregister hosregister : hosregisters) {
            RegistrationVO dto = new RegistrationVO();
            BeanUtils.copyProperties(hosregister, dto);
            if (Objects.nonNull(hosregister.getdId())) {
                Doctor doctor = doctorMapper.selectByPrimaryKey(hosregister.getdId());
                dto.setDoctor(doctor);
            }
            registrationVOList.add(dto);
        }

        // 分页信息转换
        PageInfo<RegistrationVO> pageInfo = new PageInfo<>();
        BeanUtils.copyProperties(hosregisterPageInfo, pageInfo);
        pageInfo.setList(registrationVOList);

        return pageInfo;
    }

    /**
     * 保存
     *
     * @param hosregister 保存对象
     * @return 响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Hosregister hosregister) {
        if (Objects.isNull(hosregister.getHosrId())) {
            hosregister.setHosrTime(DateTime.now());
            hosregisterMapper.insertSelective(hosregister);
        } else {
            hosregisterMapper.updateByPrimaryKey(hosregister);
        }
        return true;
    }

    /**
     * 主键查询
     *
     * @param hosrId 查询条件
     * @return 响应
     */
    public RegistrationVO selectById(Integer hosrId) {
        Hosregister hosregister = hosregisterMapper.selectByPrimaryKey(hosrId);
        Integer dId = hosregister.getdId();
        RegistrationVO registrationVO = new RegistrationVO();
        BeanUtils.copyProperties(hosregister, registrationVO);

        if (dId != null) {
            Doctor doctor = doctorMapper.selectByPrimaryKey(dId);
            registrationVO.setDoctor(doctor);
        }
        return registrationVO;
    }

    /**
     * 删除
     *
     * @param uIds 待删除 IDs
     * @return 响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Integer> uIds) {
        return true;
    }

}
